home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 7424 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.4 KB

  1. Path: news.umbc.edu!not-for-mail
  2. From: schlein@umbc.edu (Jonas J. Schlein)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: What is &Variable (declared as: char Variable[10])?
  5. Date: 26 Feb 1996 08:48:40 -0500
  6. Organization: University of Maryland Baltimore County
  7. Message-ID: <4gsdno$1bg@umbc9.umbc.edu>
  8. References: <4gqpa1$3h9@alcor.usc.edu>
  9. NNTP-Posting-Host: umbc9.umbc.edu
  10. NNTP-Posting-User: schlein
  11.  
  12. Abu Wawda <wawda@alcor.usc.edu> wrote:
  13. |> I'm having trouble understanding what the address of a static array
  14. |> is.
  15.  
  16. I'm having trouble understanding why it matters? You almost never use the
  17. address of an array directly unless doing something tricky with pointers
  18. or with particular dimensions of a multiple dimensional array.
  19.  
  20. |> For example, if I declare a variable called myarray as:
  21. |>     char myarray[10];
  22. |> then what could &myarray possibly mean? myarray is not a pointer, so
  23. |> &myarray could not possibly be the address of the variable myarray
  24. |> (like it would be if I did char* myarray and then asked for &myarray).
  25.  
  26. Yes it could and yes it is...'myarray' is not a pointer, but &myarray is
  27. a pointer to 'myarray'.
  28.  
  29. |> Functions such as scanf() allow the following:
  30. |> 
  31. |>     char myarray[10];
  32. |> 
  33. |>     scanf("%s",&myarray);
  34.  
  35. "Allow" and "produce defined results" are two different things. I believe
  36. you want simply 'scanf ("%s", myarray);'. Also make sure <stdio.h> is
  37. included.
  38.  
  39. |> but I don't understand what scanf() could possibly be taking in the
  40. |> second parameter. It can't be: char** since myarray is not a
  41. |> pointer. I CAN understand how the following would work:
  42. |> 
  43. |>     char* myarray;
  44. |> 
  45. |>     myarray = (char*) malloc(10);
  46. |>     scanf("%s",&myarray);
  47.  
  48. That's incorrect as well for several reasons...
  49.  
  50. 1) You didn't include <stdlib.h> as far as readers of comp.lang.c can tell
  51.    so your cast is possibly masking an error.
  52. 2) You don't assure malloc() does not fail.
  53. 3) Get rid of the &!!!
  54.  
  55. |> Then scanf() is simply taking a char** (pointer to a character
  56. |> pointer, or in other words the address of the pointer myarray) in its
  57. |> second paremeter. However, if I write my own function like this:
  58. |> 
  59. |>     void func(char** p)
  60. |>     {
  61. |>         // do something here
  62. |>     }    
  63. |> 
  64. |> I cannot pass &myarray if I declare it as: char myarray[10]. So how do
  65. |> this work? Thanks in advance,
  66.  
  67. Sure you can...Read the FAQ! Particularly sections on pointers and arrays.
  68.  
  69. -- 
  70. "If it wasn't for C, we would be using BASI, PASAL, and OBOL."
  71.  
  72. Jonas J. Schlein  (schlein@gl.umbc.edu)
  73.